package org.example.alicloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.example.alicloud.model.CommonResult;
import org.example.alicloud.model.Payment;
import org.example.alicloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author gaoqiang
 */
@RestController
public class CircleBreakerController {
    private static final String SERVICE_URL = "http://nacos-payment-provider";

    @Autowired
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @GetMapping("/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Integer id){
        return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
    }

    @GetMapping("/consumer/fallback/{id}")
    // @SentinelResource(value = "fallback")
//     @SentinelResource(value = "fallback",fallback = "handlerFallback")
    @SentinelResource(value = "fallback", blockHandler = "blockHandler", fallback = "handlerFallback")
    /** fallback只负责业务异常处理  blockHandler只负责sentinel控制台配置违规处理 */
    public CommonResult<Payment> fallback(@PathVariable("id") Long id){
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL+"/payment/hash/"+id,CommonResult.class,id);
        if (id == 4){
            throw new IllegalArgumentException("IllegalArgumentException 非法参数异常...");
        }else if (result.getData()==null){
            throw new NullPointerException("NullPointerException 空指针异常,id不存在");
        }

        return result;
    }
    //异常处理
    public CommonResult handlerFallback(@PathVariable("id") Long id,Throwable e){
        Payment payment = new Payment(id,"null");
        return new CommonResult(444,"handlerFallback,错误信息："+e.getMessage(),payment);
    }

    //异常处理
    public CommonResult blockHandler(@PathVariable("id") Long id,Throwable e){
        Payment payment = new Payment(id,"null");
        return new CommonResult(444,"blockHandler限流，无流水,错误信息："+e.getMessage(),payment);
    }

    //====openfeign

    @Autowired
    private PaymentService paymentService;

    @GetMapping("/consumer/payment/hash/{id}")
    @SentinelResource(value = "payment-hash", blockHandler = "blockHandler", fallback = "handlerFallback")
    public CommonResult paymentSQL(@PathVariable("id") Long id){
        CommonResult<Payment> result = paymentService.paymentSQL(id);
        if (id == 4){
            throw new IllegalArgumentException("IllegalArgumentException 非法参数异常...");
        }else if (result.getData()==null){
            throw new NullPointerException("NullPointerException 空指针异常,id不存在");
        }
        return result;
    }
}
